package cn.edu.tsinghua.training.struts.demo.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.edu.tsinghua.training.struts.demo.entity.Book;
import cn.edu.tsinghua.training.struts.demo.util.DB;

public class BookAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Book book;
	
	private static final String ADD = "insert into book values(null, ?, ?, ?, ?)";
	private static final String LIST = "select * from book";
	private static final String SEARCH = "select * from book where id=?";
	private static final String EDIT = "update book set title=?, author=?, publisher=?, time=? where id=?";
	private static final String REMOVE = "delete from book where id=?";
	
	public String add() throws Exception {
		Connection connection = DB.getConnection();
		PreparedStatement preparedStatement = connection.prepareStatement(ADD);
		System.out.println(book);
		preparedStatement.setString(1, book.getTitle());
		preparedStatement.setString(2, book.getAuthor());
		preparedStatement.setString(3, book.getPublisher());
		preparedStatement.setString(4, book.getTime());
		preparedStatement.executeUpdate();
		return "add_success";
	}
	
	public String list() throws Exception {
		Connection connection = DB.getConnection();
		PreparedStatement preparedStatement = connection.prepareStatement(LIST);
		ResultSet resultSet = preparedStatement.executeQuery();
		List<Book> books = new ArrayList<Book>();
		while (resultSet.next()) {
			Book book = new Book(resultSet.getInt("id"), resultSet.getString("title"), resultSet.getString("author"), resultSet.getString("publisher"), resultSet.getString("time"));
			books.add(book);
		}
		DB.close(resultSet, preparedStatement, connection);
		getSession().put("books", books);
		return "list_success"; 
	}
	
	public String search() throws Exception {
		Connection connection = DB.getConnection();
		PreparedStatement preparedStatement = connection.prepareStatement(SEARCH);
		preparedStatement.setInt(1, book.getId());
		ResultSet resultSet = preparedStatement.executeQuery();
		resultSet.next();
		Book book = new Book(resultSet.getInt("id"), resultSet.getString("title"), resultSet.getString("author"), resultSet.getString("publisher"), resultSet.getString("time"));
		DB.close(resultSet, preparedStatement, connection);
		getSession().put("book", book);
		return "search_success";
	}
	
	public String edit() throws Exception {
		Connection connection = DB.getConnection();
		PreparedStatement preparedStatement = connection.prepareStatement(EDIT);
		preparedStatement.setString(1, book.getTitle());
		preparedStatement.setString(2, book.getAuthor());
		preparedStatement.setString(3, book.getPublisher());
		preparedStatement.setString(4, book.getTime());
		preparedStatement.setInt(5, book.getId());
		preparedStatement.executeUpdate();
		DB.close(null, preparedStatement, connection);
		return "edit_success";
	}
	
	public String remove() throws Exception {
		Connection connection = DB.getConnection();
		PreparedStatement preparedStatement = connection.prepareStatement(REMOVE);
		preparedStatement.setInt(1, book.getId());
		preparedStatement.executeUpdate();
		return "remove_success";
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}
	
}
